from rest_framework.response import Response
from rest_framework.views import APIView

# 需要继承哪个呢？---不需要对数据进行增删改查，所以只需继承APIView
from apps.meiduo_admin.serializers.loginserializers import LoginSerializer


class LoginView(APIView):

    def post(self, request):
        serializer = LoginSerializer(data=request.data)
        # 传入raise_exception表示如果校验失败则抛出异常
        serializer.is_valid(raise_exception=True)

        return Response({
            'token': serializer.validated_data.get('token'),
            'username': serializer.validated_data['user'].username,
            'user_id': serializer.validated_data['user'].id
        })